package top.hkyzf.onekeycommand.mixin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hkyzf.onekeycommand.util.Utils;

import java.awt.event.KeyEvent;

/**
 * description: 在游戏内主页面按键时注入
 * @author zhufeng
 * @date 2022/9/7 14:57
 * @version V1.0
 */
@Mixin(KeyBinding.class)
@Environment(EnvType.CLIENT)
public class KeyBindingMixin {
    private static final MinecraftClient CLIENT = MinecraftClient.getInstance();

    @Shadow
    private InputUtil.Key boundKey;

    @Inject(method = "onKeyPressed", at = @At("HEAD"))
    private static void onKeyPressed(InputUtil.Key key, CallbackInfo ci) {
        // 当键盘被按下则执行
        Utils.LOGGER.info("KeyBinding : {} 键被按下了！", KeyEvent.getKeyText(key.getCode()));
        // 按下 F12 回默认家
        if (GLFW.GLFW_KEY_F12 == key.getCode()) {
            CLIENT.player.sendCommand("home Home");
        // 按下传送键回名字叫 safe 的家
        } else if (Utils.TRANSPORT_KEY.equals(key.getTranslationKey())) {
            CLIENT.player.sendCommand("home safe");
        // 按下挂机键开始挂机
        } else if (Utils.ON_HOOK_KEY.getBoundKeyTranslationKey().equals(key.getTranslationKey())) {
            Utils.LOGGER.info("开始挂机！");
        }
    }
}
